package com.products.utils;

import java.util.GregorianCalendar;

import com.products.gui.Frame;

public class TimeThinker implements Runnable {
	private static final long serialVersionUID = 1L;
	private SimpleMail mailSender = new SimpleMail();
	private Frame frame = null;

	public TimeThinker(Frame frame) {
		this.frame = frame;
	}

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(86400);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar
					.getInstance();
			int lastDayOfMonth = calendar.get(GregorianCalendar.DAY_OF_MONTH);
			int currentDayOfMonth = calendar
					.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
			if (lastDayOfMonth == currentDayOfMonth) {
				String respEmail = frame.getCommands().getMail();
				mailSender.Send(frame.getProducts(), respEmail);
				frame.getCommands().removeOrder();
			}
		}
	}
}
